package com.letv.core.sysletvplayer.base.impl;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.BatteryManager;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.FrameLayout;

//import com.letv.android.remotedevice.Constant;
//import com.letv.android.remotedevice.DeviceInfo;
import com.letv.android.sdk.R;
import com.letv.core.sysletvplayer.Interface.SystemInfoListener;
import com.letv.core.sysletvplayer.base.BaseActivity;
import com.letv.core.sysletvplayer.view.LetvGestureLayout;

public class LocalPlayerActivity extends BaseActivity {

	public final static int REQUEST_CODE = 1001;
	
	/**
	 * 播放器之上的容器
	 * */
	protected FrameLayout playRootLayout;
	/**
	 * 播放器的Fragment
	 * */
	protected com.letv.core.sysletvplayer.base.impl.LocalPlayerFragment playFragment;
	/**
	 * 播放器之上的手势层
	 * */
	protected LetvGestureLayout playGestrue;
	/**
	 * 播放器之上的容器
	 * */
	protected FrameLayout playUpper;
	/**
	 * 控制器
	 */
	protected LocalPlayerController mPlayControler;
	
	protected SystemInfoListener systemInfoListener;
	
	/**
	 * 启动本地播放器
	 * @param context
	 * @param url
	 */
	public static void launcher(Context context, String url) {
		Intent intent = new Intent(context, LocalPlayerActivity.class);
		intent.putExtra("url", url);
		context.startActivity(intent);
	}
	
	/**
	 * 启动本地播放器
	 * @param context
	 * @param url
	 */
	public static void launcher(Context context, String url, int msec) {
		Intent intent = new Intent(context, LocalPlayerActivity.class);
		intent.putExtra("url", url);
		intent.putExtra("seek", msec);
		if (!(context instanceof Activity)) {
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		}
		context.startActivity(intent);
	}

	/**
	 * 启动本地播放器(为随便看提供，需要传入标题)
	 * @param context 上下环境
	 * @param url  本地播放路径
	 * @param title  要显示视频的标题
	 */
	public static void launcher(Context context, String url, String title) {
		Intent intent = new Intent(context, LocalPlayerActivity.class);
		intent.putExtra("url", url);
		intent.putExtra("title", title);
		if (!(context instanceof Activity)) {
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		}
		context.startActivity(intent);
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.i("chenyg", "LocalPlayerActivity--->onCreate()");
		setContentView(R.layout.letv_local_player_main);

		findView();
		
		initView();
	}
	
	/**
	 * 初始化控件
	 * */
	private void findView() {
		playRootLayout = (FrameLayout) findViewById(R.id.play_root);
		playUpper = (FrameLayout) findViewById(R.id.play_upper);
		playFragment = (LocalPlayerFragment) this.getSupportFragmentManager().findFragmentById(R.id.play_fragment);
		playGestrue = (LetvGestureLayout) findViewById(R.id.play_gestrue);
	}
	
	private void initView() {
		mPlayControler = new LocalPlayerController(this);
//		mPlayPannel = new LocalPlayerPannel(this);
//		mPlayPannel.setPlayerController(mPlayControler);
//		setContentView(mPlayPannel.getContentView());
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onResume() {
		super.onResume();
		//启动本地播放器时，关闭小窗，以防出现两个播放界面
		closePipView();
		registerReceiver();
	}

	@Override
	protected void onPause() {
		super.onPause();
		unregisterReceiver();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_VOLUME_MUTE) {
			Log.i("chenyg", "KeyEvent.KEYCODE_VOLUME_MUTE");
			return true;
		}
		if (mPlayControler != null) {
			int curSoundVolume = mPlayControler.getCurSoundVolume();
			int maxSoundVolume = mPlayControler.getMaxSoundVolume();
//			Log.i("chenyg", "onKeyDown()---->curSoundVolume=" + curSoundVolume + ", maxSoundVolume=" + maxSoundVolume);
			int temp = curSoundVolume;
			if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
				temp -= 1;
				if(temp < 0) {
					temp = 0;
				}
			} else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
				temp += 1;
				if(temp > maxSoundVolume) {
					temp = maxSoundVolume;
				}
			}
//			Log.i("chenyg", "onKeyDown()---->temp=" + temp + ", maxSoundVolume=" + maxSoundVolume);
			mPlayControler.changeVolumeByTouch(temp, maxSoundVolume);
		}
		return super.onKeyDown(keyCode, event);
	}
	
	

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		
		  if (arg0 == REQUEST_CODE && arg1 == RESULT_OK) {
	          //返回DeviceInfo数据
//	            DeviceInfo info = arg2.getParcelableExtra(Constant.EXTRA_DEVICE_INFO);
//	            mPlayControler.pushPlay(info);
	        }
	        super.onActivityResult(arg0, arg1, arg2);
	}

	public LocalPlayerFragment getPlayFragment() {
		return playFragment;
	}

	public FrameLayout getPlayRootLayout() {
		return playRootLayout;
	}

	public FrameLayout getPlayUpper() {
		return playUpper;
	}

	public LetvGestureLayout getPlayGestrue() {
		return playGestrue;
	}

	public LocalPlayerController getmPlayControler() {
		return mPlayControler;
	}
	
	public void SetSystemInfoListener(SystemInfoListener l) {
		this.systemInfoListener = l;
	}
	
	/**
	 * 关闭小窗播放
	 */
	private void closePipView(){
		Intent serviceStop = new Intent();
		serviceStop.setAction("com.letv.android.pipService");
		this.stopService(serviceStop);
	}
	
	/**
	 * 注册关于监听 电量，时间，网络状态的监听
	 * */
	private void registerReceiver() {
		try{
			IntentFilter filter = new IntentFilter();
			filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
			filter.addAction(Intent.ACTION_TIME_TICK);
			filter.addAction(Intent.ACTION_BATTERY_CHANGED);
			filter.addAction(Intent.ACTION_SCREEN_ON);
			filter.addAction(Intent.ACTION_SCREEN_OFF);
			filter.addAction(Intent.ACTION_USER_PRESENT);
			registerReceiver(mBroadcastReceiver, filter);
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	/**
	 * 取消关于监听 电量，时间，网络状态的监听
	 * */
	private void unregisterReceiver() {
		try{
			unregisterReceiver(mBroadcastReceiver);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 监听网络，电量，时间
	 * */
	private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (Intent.ACTION_TIME_TICK.equals(intent.getAction())) {// 监听每分钟的时间变化
				if (systemInfoListener != null) {
					systemInfoListener.onTimeChange();
				}
			} else if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent
					.getAction())) {// 监听网络连接状态变化
				if (systemInfoListener != null) {
					systemInfoListener.onNetChange();
				}
			} else if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {// 监听电量变化
				int status = intent.getIntExtra("status",
						BatteryManager.BATTERY_STATUS_UNKNOWN);// 获得电池状态
				int level = intent.getExtras().getInt("level", 0);// 获得当前电量
				int scale = intent.getExtras().getInt("scale", 100);// 获得总电量
				int curPower = level * 100 / scale;
				if (systemInfoListener != null) {
					systemInfoListener.onBatteryChange(status, curPower);
				}
			} else if (Intent.ACTION_SCREEN_ON.equals(intent.getAction())) {// 开屏
				if (systemInfoListener != null) {
					systemInfoListener.onScreenOn();
				}
			} else if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {// 锁屏
				if (systemInfoListener != null) {
					systemInfoListener.onScreenOff();
				}
			} else if (Intent.ACTION_USER_PRESENT.equals(intent.getAction())) {// 解锁
				if (systemInfoListener != null) {
					systemInfoListener.onUserPersent();
				}
			}
		}
	};
	
}
